package com.ruoyi.zsyh.controller;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zsyh.domain.ZsyhServiceType;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsyh.domain.ZsyhServiceTypeDetail;
import com.ruoyi.zsyh.service.IZsyhServiceTypeDetailService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 服务分类详细Controller
 * 
 * @author yuexl
 * @date 2022-02-16
 */
@Controller
@RequestMapping("/zsyh/serviceTypeDetail")
public class ZsyhServiceTypeDetailController extends BaseController
{
    private String prefix = "zsyh/serviceTypeDetail";

    @Autowired
    private IZsyhServiceTypeDetailService zsyhServiceTypeDetailService;

    @RequiresPermissions("zsyh:serviceTypeDetail:view")
    @GetMapping()
    public String serviceTypeDetail()
    {
        return prefix + "/serviceTypeDetail";
    }

    @RequiresPermissions("zsyh:serviceTypeDetail:view")
    @GetMapping("/nextType")
    public String nextType()
    {
        return prefix + "/nextType";
    }

    /**
     * 查询服务分类详细列表
     */
    @RequiresPermissions("zsyh:serviceTypeDetail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZsyhServiceTypeDetail zsyhServiceTypeDetail)
    {
        startPage();
        List<ZsyhServiceTypeDetail> list = zsyhServiceTypeDetailService.selectZsyhServiceTypeDetailList(zsyhServiceTypeDetail);
        return getDataTable(list);
    }

    /**
     * 查询服务分类详细列表
     */
    @RequiresPermissions("zsyh:serviceTypeDetail:list")
    @PostMapping("/listSecond")
    @ResponseBody
    public TableDataInfo listSecond(ZsyhServiceTypeDetail zsyhServiceTypeDetail)
    {
        startPage();
        List<ZsyhServiceTypeDetail> list = zsyhServiceTypeDetailService.selectZsyhServiceTypeDetailSecondList(zsyhServiceTypeDetail);
        return getDataTable(list);
    }

    /**
     * 查询服务分类详细列表
     */
    @RequiresPermissions("zsyh:serviceTypeDetail:list")
    @PostMapping("/listThird")
    @ResponseBody
    public TableDataInfo listThird(ZsyhServiceTypeDetail zsyhServiceTypeDetail)
    {
        startPage();
        List<ZsyhServiceTypeDetail> list = zsyhServiceTypeDetailService.selectZsyhServiceTypeDetailThirdList(zsyhServiceTypeDetail);
        return getDataTable(list);
    }

    /**
     * 导出服务分类详细列表
     */
    @RequiresPermissions("zsyh:serviceTypeDetail:export")
    @Log(title = "服务分类详细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZsyhServiceTypeDetail zsyhServiceTypeDetail)
    {
        List<ZsyhServiceTypeDetail> list = zsyhServiceTypeDetailService.selectZsyhServiceTypeDetailList(zsyhServiceTypeDetail);
        ExcelUtil<ZsyhServiceTypeDetail> util = new ExcelUtil<ZsyhServiceTypeDetail>(ZsyhServiceTypeDetail.class);
        return util.exportExcel(list, "服务分类详细数据");
    }

    /**
     * 新增服务分类详细
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存服务分类详细
     */
    @RequiresPermissions("zsyh:serviceTypeDetail:add")
    @Log(title = "服务分类详细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZsyhServiceTypeDetail zsyhServiceTypeDetail)
    {//设置级别
        if("0".equals(zsyhServiceTypeDetail.getParentId())){
            zsyhServiceTypeDetail.setServiceLevel("1");
        }else{
            ZsyhServiceTypeDetail parentType = zsyhServiceTypeDetailService.selectZsyhServiceTypeDetailById(zsyhServiceTypeDetail.getParentId());
            if("0".equals(parentType.getParentId())){
                zsyhServiceTypeDetail.setServiceLevel("2");
            }else{
                zsyhServiceTypeDetail.setServiceLevel("3");
            }
        }
        return toAjax(zsyhServiceTypeDetailService.insertZsyhServiceTypeDetail(zsyhServiceTypeDetail));
    }

    /**
     * 修改服务分类详细
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhServiceTypeDetail zsyhServiceTypeDetail = zsyhServiceTypeDetailService.selectZsyhServiceTypeDetailById(id);
        mmap.put("zsyhServiceTypeDetail", zsyhServiceTypeDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存服务分类详细
     */
    @RequiresPermissions("zsyh:serviceTypeDetail:edit")
    @Log(title = "服务分类详细", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZsyhServiceTypeDetail zsyhServiceTypeDetail)
    {
        //设置级别
        if("0".equals(zsyhServiceTypeDetail.getParentId())){
            zsyhServiceTypeDetail.setServiceLevel("1");
        }else{
            ZsyhServiceTypeDetail parentType = zsyhServiceTypeDetailService.selectZsyhServiceTypeDetailById(zsyhServiceTypeDetail.getParentId());
            if("0".equals(parentType.getParentId())){
                zsyhServiceTypeDetail.setServiceLevel("2");
            }else{
                zsyhServiceTypeDetail.setServiceLevel("3");
            }
        }
        zsyhServiceTypeDetail.setServiceName(zsyhServiceTypeDetail.getThirdServiceName());
        return toAjax(zsyhServiceTypeDetailService.updateZsyhServiceTypeDetail(zsyhServiceTypeDetail));
    }

    /**
     * 删除服务分类详细
     */
    @RequiresPermissions("zsyh:serviceTypeDetail:remove")
    @Log(title = "服务分类详细", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        //判断有下级的话不能删除
        ZsyhServiceTypeDetail zsyhServiceType = new ZsyhServiceTypeDetail();
        zsyhServiceType.setParentId(ids);
        List<ZsyhServiceTypeDetail> zsyhServiceTypeList = zsyhServiceTypeDetailService.selectZsyhServiceTypeDetailList(zsyhServiceType);
        if(zsyhServiceTypeList.size()>0){
            return error("该分类存在下级分类，不能删除!");
        }
        return toAjax(zsyhServiceTypeDetailService.deleteZsyhServiceTypeDetailByIds(ids));
    }

    /**
     * 新增服务分类
     */
    @GetMapping("/addSecond")
    public String addSecond(ModelMap mmap)
    {
        ZsyhServiceTypeDetail zsyhServiceTypeDetail = new ZsyhServiceTypeDetail();
        zsyhServiceTypeDetail.setParentId("0");
        mmap.put("parents",zsyhServiceTypeDetailService.selectZsyhServiceTypeDetailList(zsyhServiceTypeDetail));
        return prefix + "/addSecond";
    }

    /**
     * 新增服务分类
     */
    @GetMapping("/addThird")
    public String addThird( ModelMap mmap)
    {
        return prefix + "/addThird";
    }

    /**
     * 修改服务分类详细
     */
    @GetMapping("/editSecond/{id}")
    public String editSecond(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhServiceTypeDetail zsyhServiceTypeDetail = zsyhServiceTypeDetailService.selectZsyhServiceTypeDetailById(id);
        mmap.put("zsyhServiceTypeDetail", zsyhServiceTypeDetail);
        ZsyhServiceTypeDetail zsyhServiceTypeDetail1 = new ZsyhServiceTypeDetail();
        zsyhServiceTypeDetail1.setParentId("0");
        List<ZsyhServiceTypeDetail> zsyhServiceTypeDetailList = zsyhServiceTypeDetailService.selectZsyhServiceTypeDetailList(zsyhServiceTypeDetail1);
        for(ZsyhServiceTypeDetail temp : zsyhServiceTypeDetailList){
            if(zsyhServiceTypeDetail.getParentId().equals(temp.getId())){
                temp.setFlag(true);
            }
        }
        mmap.put("parents",zsyhServiceTypeDetailList);
        return prefix + "/editSecond";
    }

    /**
     * 修改服务分类详细
     */
    @GetMapping("/editThird/{id}")
    public String editThird(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhServiceTypeDetail zsyhServiceTypeDetail = zsyhServiceTypeDetailService.selectZsyhServiceTypeDetailById(id);
        mmap.put("zsyhServiceTypeDetail", zsyhServiceTypeDetail);
        return prefix + "/editThird";
    }

    /**
     * 校验用户名
     */
    @PostMapping("/checkServiceNameUnique")
    @ResponseBody
    public String checkServiceNameUnique(String serviceName,String parentId,String serviceLevel)
    {
        ZsyhServiceTypeDetail zsyhServiceTypeDetail = new ZsyhServiceTypeDetail();
        if("1".equals(serviceLevel)){
            //一级服务类型
            zsyhServiceTypeDetail.setServiceName(serviceName);
            zsyhServiceTypeDetail.setServiceLevel("1");
            zsyhServiceTypeDetail.setParentId("0");
            if(zsyhServiceTypeDetailService.selectZsyhServiceTypeDetailList(zsyhServiceTypeDetail).size()>0){
                return UserConstants.USER_NAME_NOT_UNIQUE;
            }else{
                return UserConstants.USER_NAME_UNIQUE;
            }
        } else {
            //二级或三级服务类型
            zsyhServiceTypeDetail.setServiceLevel(serviceLevel);
            zsyhServiceTypeDetail.setParentId(parentId);
            zsyhServiceTypeDetail.setServiceName(serviceName);
            if (zsyhServiceTypeDetailService.selectZsyhServiceTypeDetailList(zsyhServiceTypeDetail).size() > 0) {
                return UserConstants.USER_NAME_NOT_UNIQUE;
            } else {
                return UserConstants.USER_NAME_UNIQUE;
            }
        }
    }
}
